<template>
  <div class="mod-menu main-container">
    <div class="search-bar">
      <el-form
        @submit.native.prevent
        :inline="true"
        class="search-form"
        ref="searchForm"
        :model="searchForm"
        size="small"
      >
        <div class="input-row">
          <el-form-item
            prop="keyword"
            label="关键词"
            label-width="80px"
            class="search-form-item"
          >
            <el-input
              type="text"
              v-model="searchForm.keyword"
              placeholder="请输入关键词"
            ></el-input>
          </el-form-item>
          <div
            class="default-btn primary-btn marginBottom"
            @click="searchChange(true)"
          >
            {{ $t("product.search") }}
          </div>
          <div class="default-btn" @click="resetSearchForm('searchForm')">
            {{ $t("product.reset") }}
          </div>
        </div>
      </el-form>
    </div>

    <!-- 操作栏 -->
    <div class="operation-bar">
      <div class="default-btn primary-btn" @click="addBannedWords()">
        {{ $t("sysManagement.add") }}
      </div>
    </div>

    <!-- 表格 -->
    <div class="table-con sys-menu-table">
      <el-table
        :data="dataList"
        header-cell-class-name="table-header"
        style="width: 100%"
      >
        <el-table-column
          prop="name"
          label="关键词名称"
        >
        </el-table-column>
        <!-- <el-table-column
          prop="orderNum"
          align="center"
          :label="$t('sys.orderNum')"
        >
          <template slot-scope="scope">
            {{ scope.row.orderNum }}
          </template>
        </el-table-column> -->
        <!-- <el-table-column prop="disabled" align="center" label="是否屏蔽">
          <template slot-scope="scope">
            <span v-if="scope.row.disabled">{{ $t("sys.yes") }}</span>
            <span v-else>{{ $t("sys.no") }}</span>
          </template>
        </el-table-column> -->
        <el-table-column
          fixed="right"
          align="center"
          :label="$t('sys.operation')"
        >
          <template slot-scope="scope">
            <div class="text-btn-con">
              <div
                class="default-btn text-btn"
                @click="deleteHandle(scope.row)"
              >
                解除屏蔽
              </div>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <el-pagination
      style="margin-top: 12px; text-align: right"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="params.current"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="params.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="params.total"
    >
    </el-pagination>

    <!-- 弹窗, 新增 / 修改 -->
    <AddOrUpdateBannedWords
      v-if="addOrUpdateBannedWords"
      ref="AddOrUpdateBannedWords"
      @refreshDataList="getDataList"
    />
  </div>
</template>

<script>
import AddOrUpdateBannedWords from "./addOrUpdateBannedWords.vue";
export default {
  data() {
    return {
      searchForm: {},
      params: {
        current: 1,
        size: 10,
        total: 0,
      },
      dataList: [],
      dataListLoading: false,
      addOrUpdateBannedWords: false,
    };
  },
  components: {
    AddOrUpdateBannedWords,
  },
  created() {
    this.getDataList();
  },
  methods: {
    // 条件查询
    searchChange(newData = false) {
      this.getDataList(this.params.current, newData);
    },
    /**
     * 重置表单
     * @param {String} formName 表单名称
     */
    resetSearchForm(formName) {
      this.$refs[formName].resetFields();
      this.searchForm = {};
      this.params = {
        current: 1,
        size: 10,
      };
      this.searchReset();
    },
    // 新增 / 修改
    addBannedWords(id) {
      this.addOrUpdateBannedWords = true;
      this.$nextTick(() => {
        this.$refs.AddOrUpdateBannedWords.init(id);
      });
    },
    // 获取数据列表
    getDataList() {
      this.$http({
        url: this.$http.adornUrl("/admin/ShieldingWord/page"),
        method: "post",
        params: this.$http.adornParams({
          ...this.searchForm,
          ...this.params,
        }),
      }).then(({ data }) => {
        this.dataList = data.records;
        this.params.total = data.total;
      });
    },
    // 分页每页多少条
    handleSizeChange(val) {
      this.params.size = val;
      this.getDataList();
    },
    // 分页当前第几页
    handleCurrentChange(val) {
      this.params.current = val;
      this.getDataList();
    },
    // 删除
    deleteHandle(row) {
      this.$confirm(
        this.$i18n.t("seckill.isDeleOper").replace("删除", "解除屏蔽"),
        this.$i18n.t("text.tips"),
        {
          confirmButtonText: this.$i18n.t("coupon.confirm"),
          cancelButtonText: this.$i18n.t("coupon.cancel"),
          type: "warning",
        }
      ).then(() => {
        this.$http({
          url: this.$http.adornUrl(
            `/admin/ShieldingWord/updateShieldingWord/${row.id}`
          ),
          method: "get",
          data: this.$http.adornData(),
        }).then(({ data }) => {
          this.$message({
            message: this.$i18n.t("publics.operation"),
            type: "success",
            duration: 1500,
          });
          this.getDataList();
        });
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.mod-menu.main-container {
  padding-top: 0;
}
</style>
